#! /usr/bin/python
ident = '$Id: publications.py 109 2006-06-05 18:46:37Z trevor $'
'''Defines the base rex objects class. All rex objects are derived from this class to
standerdize the xml serialization process.
Child classes define their own data dictionary with the properties they are interested in.
'''
import sys, common, base
from   	xml.sax 	import saxutils
from 	xml.dom		import minidom

################################
class publication( base.object ):
	'''Represents the rex publication objects'''
	def __init__( self, account, module, xmlstring = None ):
		assert account, 'No account passed'
		assert module, 'No module'
		self.data = []
		# layout options
		lops = [ ('NL','No Layout' ), ('C','Content' ), ('LC','Left Column, Content'), ('CR','Content, Right Column'), ('LCR','Left Column, Content, Right Column') ]
		dops = [ ('immediate','Immediate'), ('timed','Timed'), ('dated','Dated'), ('recurring','Recurring') ]
		self.data.append( {'name':'id',			'value':'', 'label':'ID', 		'type':base.ROT_NUMBER,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'name',		'value':'', 'label':'Name',		'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'title',		'value':'', 'label':'Title',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'description','value':'', 'label':'Description','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'layout',		'value':'NL','label':'Layout',	'type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,	'options':lops,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'header',		'value':'', 'label':'Header','type':base.ROT_TEXT,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'footer',		'value':'', 'label':'Footer','type':base.ROT_TEXT,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'created_on',	'value':'', 'label':'Created On','type':base.ROT_DATE,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'delivery',	'value':'immediate', 'label':'Delivery','type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,	'options':dops,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'reply_email','value':'', 'label':'Reply Address',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'cc_email',	'value':'', 'label':'CC Address',		'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bcc_email',	'value':'', 'label':'BCC Address',		'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bouncer',	'value':'0','label':'Bouncer',			'type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,	'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bouncer_email','value':'', 'label':'Bouncer Address','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bouncer_group','value':'', 'label':'Bouncer Group',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.object_name = "publication"
		self.issues = []
		base.object.__init__( self, account, module, xmlstring )

	def fromxml( self, xmlstring ):
		'''Expected DOM example
		<publication id='100' name='My Publication' >
			<issue name='issue1' id='130' status='active' />
			<issue name='issue2' id='140' status='archived' />
		</publication>'''
		# Call the base method to handle the attributes, then we parse the issues
		base.object.fromxml( self, xmlstring )
		# Now parse all the issues
		xmlstring = common.uni2str( xmlstring )
		try:
			sdom 	= minidom.parseString( xmlstring )
			snode	= sdom.firstChild
			for inode in snode.getElementsByTagName("issue"):
				self.issues.append( issue( self.account, self, inode.toxml() ) )
		except Exception:
			common.show_error( "Failed to load publication issues from xml." )

class issue( base.object ):
	'''Represents the issue class'''
	def __init__( self, account, publication, xmlstring = None ):
		assert account, 'No account passed'
		assert publication, 'No publication'
		self.data = []
		self.module = publication.module
		self.data.append( {'name':'id',				'value':'',	'label':'ID', 		'type':base.ROT_NUMBER,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'publication_id',	'value':'', 'label':'Folder ID','type':base.ROT_NUMBER,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'name',			'value':'', 'label':'Name',		'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'title',			'value':'', 'label':'Title',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'description',	'value':'', 'label':'Description','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'left',			'value':'', 'label':'Left','type':base.ROT_TEXT,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'content',		'value':'', 'label':'Content','type':base.ROT_TEXT,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'right',			'value':'', 'label':'Right','type':base.ROT_TEXT,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'status',			'value':'',	'label':'Status',	'type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,	'options':[ ('activated','Activated'), ('deactivated','Deactivated'), ('archived','Archived') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'delivery_day','value':'', 'label':'Delivery Day', 'type':base.ROT_NUMBER,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'delivery_date','value':'', 'label':'Delivery Date', 'type':base.ROT_DATE,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'recurrence_rule','value':'', 'label':'Recurrence Rule', 'type':base.ROT_FREQ,'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'recurrence_var','value':'', 'label':'Recurrence Variable','type':base.ROT_TEXT,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'reply_email','value':'', 'label':'Reply Address',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'cc_email',	'value':'', 'label':'CC Address',		'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bcc_email',	'value':'', 'label':'BCC Address',		'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bouncer',	'value':'0','label':'Bouncer',			'type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,	'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bouncer_email','value':'', 'label':'Bouncer Address','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bouncer_group','value':'', 'label':'Bouncer Group',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'created_on',		'value':'', 'label':'Created On','type':base.ROT_DATE,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.object_name = "issue"
		self.publication = publication
		base.object.__init__( self, account, self.module, xmlstring )

	#def fromxml( self, xmlstring ):
		# Call the base method to handle the attributes, then we parse the issues
	#	base.object.fromxml( self, xmlstring )
